package com.it.service.impl;

import com.it.VO.MessageCommentOV;
import com.it.dao.MessageCommentMapper;
import com.it.domain.MessageComment;
import com.it.domain.User;
import com.it.service.MessageCommentService;
import com.it.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageCommentServImpl implements MessageCommentService {

    @Autowired
    private MessageCommentMapper messageCommentMapper;

    @Autowired
    private UserService userService;

    @Override
    public boolean saveComment(MessageComment messageComment) {

        return messageCommentMapper.saveComment(messageComment)>0;

    }

    @Override
    public List<MessageCommentOV> SelectAll() {

        List<MessageComment> msCom = messageCommentMapper.SelectAll();
        if(msCom == null){
            return null;
        }
        List<MessageCommentOV> messageCommentOVList = new ArrayList<>();
        for(MessageComment msg : msCom){
            MessageCommentOV mesComOv = new MessageCommentOV();
            User user = userService.SelectById(msg.getUserId());
            mesComOv.setContent(msg.getContent());
            mesComOv.setImage(user.getImage());
            mesComOv.setUserId(msg.getUserId());
            mesComOv.setUsername(user.getUsername());
            mesComOv.setParentId(msg.getParentId());
            mesComOv.setRootId(msg.getRootId());
            mesComOv.setTime(msg.getTime());
            mesComOv.setId(msg.getId());
            mesComOv.setIsDelete(msg.getIsDelete());
            if(msg.getParentId() != -1) {
                mesComOv.setToCommentUSerName(userService.SelectById(messageCommentMapper.SelectById(msg.getParentId()).getUserId()).getUsername());
            }
            messageCommentOVList.add(mesComOv);
        }
        return messageCommentOVList;
    }

    @Override
    public boolean DeleteRootComment(Integer rootId) {
        return messageCommentMapper.DeleteRootComment(rootId)>0;
    }

    @Override
    public boolean DeleteComment(Integer id) {
        return messageCommentMapper.DeleteComment(id)>0;
    }

    @Override
    public List<MessageComment> SelectByParentId(Integer id) {
        return messageCommentMapper.SelectByParentId(id);
    }

    @Override
    public MessageComment SelectById(Integer id) {
        return messageCommentMapper.SelectById(id);
    }

}
